<?php
/*
 * App顶部图片管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-26
 */
namespace Admin\Controller;

class TopphotoController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize(){       
        parent::_initialize();
        $this->db   =new \Model\AdModel();
    }
    
    /*
     * 顶部图片列表
     * 
     * @return #
     */
    public function index(){
        if(I("post.name")){
            $map['name']=I("post.name","","trim");
        }
        $map            =$this->_search("Ad");
        $map['state']   =array("lt",3);
        $photoList      =$this->_list($this->db,$map,'id',FALSE);
        $this->assign("photoList",$photoList);
        $this->display();
    }
    
    /*
     * 显示编辑图片
     * 
     * @return #
     */
    public function show(){
        $id =I("request.id","","int");               
        if($id){
            $_SESSION['ad_image']   =I("request.id","","int"); 
            $photo  =$this->db->find($id);
            $this->assign("photo",$photo);
        }
        $this->display();
    }
    
    /*
     * 图片编辑
     * 
     * @return bool
     */
    /*
    public function edit(){
        if(I("request.id")){
            $map['id']  =I("request.id","","int");
        }
        if(I("request.name")){
            $map['name']=I("request.name","","trim");
        }
        if(I("request.contentUrl")){
            $map['contentUrl']  =I("request.contentUrl");
        }
        $map['type']    =I("request.type");
        if(!empty($_FILES['image']['name'])){
            //编辑用户，如果头像更改，则删除原头像，减少空间占用
            $photo              =D("Ad")->where(array("id"=>I("request.id","","int")))->getField("image");
            $_SESSION['image']  =$photo;
            $config =array(
                'rootPath'  =>'./Application/Public/',                              //根目录
                'savePath'  =>'upload/',                                            //保存路径
                'maxSize'   =>'502400',                                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['image']);
            if(!$upload){
                 $new_upload->getError();           //获得上传附件产生的错误信息
                 $this->error();
            }else{
                //拼装图片的路径名
                $upimg          =$upload['savepath'].$upload['savename'];
                $map['image']   =$upimg;                                         
            }
        }
        $photo  =$this->user->edit($map);                   
        if($userEdit){ 
            unlink('Application/Public/'.$_SESSION['image']); 
            $this->success("编辑成功");                
        }else{
            $this->error("编辑失败");
        }
    }
     * 
     */
    
    /*
     * 删除顶部图片
     * 
     * @return #
     */
    public function del(){        
        if(!I("request.id")){
            $this->error("参数错误");
        }
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db->del($map)){
            $this->success("图片删除成功");
        }else{
            $this->error("图片删除失败");
        }
    }
    
    /*
     * 禁用、启用照片
     * 
     * @return #
     */
    public function open(){
        if(!I("request.id")|| !I("request.state")){
            $this->error("参数错误");
        }
        if(I("request.state")==1){
            $map['state']   =2;
        }else if(I("request.state")==2){
            $map['state']   =1;
        }
        $map['id']          =I("request.id");
        if($this->db->open($map)){
            if($map['state']==2){
                 $this->success("图片启用成功");
            }else if($map['state']==1){
                $this->success("图片禁用成功");
            }           
        }else{
            if($map['state']==2){
                 $this->success("图片启用失败");
            }else if($map['state']==1){
                $this->success("图片禁用失败");
            } 
        }
    }
    
    /*
     * 处理图片
     * 
     */
    public function upPhoto(){ 
        if(!empty($_FILES['images']['name'])){
            $config =array(
                'rootPath'  =>'./Application/Public/upload/',                              //根目录
                'savePath'  =>'ad/',                                            //保存路径
                'maxSize'   =>'902400',                                             //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('jpg','png','gif','jpeg',"JPG","PNG","JPEG","GIF")    //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['images']);
            if(!$upload){
                echo $new_upload->getError();
            }else{                
                echo ($upload['savepath'].$upload['savename']);                              
            }           
        }
    }
    
    /*
     * 
     * 上传表单
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']  =I("request.id");            
        }
        if(I("request.name")){
            $map['name']        =I("request.name");
        }
        if(I("request.type")){
            $map['type']        =I("request.type");
        }
        if(I("request.contentUrl")){
            $map['contentUrl']  =I("request.contentUrl");
        }
        if(I("request.image")){
            $map['image']       =I("request.image");
        }
        if($this->db->edit($map)){            
            $this->success("广告编辑成功");
        }else{            
            $this->error("广告编辑失败");
        }
    }
}